// 初始化调试工具

const vConsole = new window.VConsole();
setTimeout(()=>{
    vConsole.setSwitchPosition(0,500)
},300)

// 设置地图的安全密钥
window._AMapSecurityConfig  ={
    securityJsCode: "c60c7bd4e3f2028e1a79514eee751d82"
}
var hwMap;
var AMapIns;
var nowMark;
var nowLayer;
var siteServer;
var h5port;

AMapLoader.load({
    key: "c4a9fc30889509165c7ae94811bdb5c9",
    version: "2.0"
}).then( async AMap=>{
    AMapIns = AMap;''

    hwMap = new AMap.Map("map",{
        viewMode: '2d' ,// 默认使用2d地图
        zoom: 16, // 地图的默认缩放等级
        center:  [113.268554, 23.132943]
    })

})




const handleMessage = (event)=>{
    console.log('从ets发送过来的数据',event.data);
    const { type,payload} = JSON.parse(event.data);
    if(type === 'setCenter'){
        const gcjloc = coordtransform.wgs84togcj02(payload.lng,payload.lat)
        const centerPoint = new AMapIns.LngLat(gcjloc[0],gcjloc[1])
        hwMap.setCenter(centerPoint);
        hwMap.setZoom(16);
        nowLayer.setPosition(centerPoint)
        // gps的坐标信息 不能直接是用在 地图框架当中 需要进行一定的转换
        // AMapIns.convertFrom([payload.lng, payload.lat], 'gps', (_,result)=>{
        //     const centerPoint = result.locations[0]; // gps坐标转换之后的坐标点
        //     hwMap.setCenter(centerPoint);
        //     hwMap.setZoom(16);
        //     nowLayer.setPosition(centerPoint)
        // })
    }

    if(type === 'zoom-'){
        hwMap.zoomOut();
        hwMap.resize();
    }
    if(type === 'zoom+'){
        hwMap.zoomIn();
        hwMap.resize();
    }
    // 轨迹划线
    if(type==='drawLine'){
        const startLoc = payload.startLocationInfo.location.split(',');
        const endLoc = payload.endLocationInfo.location.split(',');
        AMapIns.plugin('AMap.Driving',()=>{
            const driving = new AMapIns.Driving({
                map: hwMap
            })
            driving.search(startLoc,endLoc,function(staus,result){
                    const routerInfo = result.routes[0];
                    h5port.postMessage(JSON.stringify({
                        type:'countFee',
                        payload: routerInfo
                    }))
                    hwMap.on('zoomend',()=>{
                        const cp = hwMap.getCenter();
                        const { distance ,time} = routerInfo;
                        const infoWindow = new AMapIns.InfoWindow({
                            content: `<div>
                                            <div> ${distance <500 ? distance +'米': parseFloat( distance/1000, 2)+'公里' }  </div>
                                            <div>  ${Math.round(time / 60) } 分</div>
                                      </div>`,
                            position: cp
                        })
                        infoWindow.open(hwMap);
                        hwMap.off('zoomend')
                    })
            })
        })
    }

}


//  响应postMessage事件
window.addEventListener('message', function(event){
    console.log('与应用侧建立了通讯');
    if(event.data === '__init_port__' ){
        h5port = event.ports[0];
        h5port.onmessage = handleMessage
        h5port.postMessage(JSON.stringify({type:'webready'}))
    }
})